<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * use Common\Model 这块可以不需要使用，框架默认会加载里面的内容
 */
class LoginController extends Controller {

    public function index(){
        if(session('adminUser')){
           $this->redirect('/admin.php?c=index');       
        }
        $this->display();
    }
    public function check(){
        /*用户登录方法*/
        $arr['username']=trim(htmlspecialchars(I("POST.username")));
        $arr['password']=trim(I("POST.password"));
        if(!$arr['username']){
            $this->ajaxReturn(show(0,"用户名不能为空"),"JSON");
        }
        if(!$arr['password']){
            $this->ajaxReturn(show(0,"密码不能为空"),"JSON");
        }
       $res=D('Admin')->getAdminByUsername($arr['username']);
       if(!$res){
           return show(0,"该用户不存在");
       }
      if($res['password']!=getMd5Password($arr['password'])){
           return show(0,"密码错误");
       }/*
        
            //返回根据用户名查找到的数据库加密密码字符串
            //admin用户名的初始密码为adminsing_cms
            $this->ajaxReturn($res['password'],'json'); */
      
       //如果都满足条件则返回成功,
       session("adminUser",$res);
       //$this->ajaxReturn(session('adminuser.username'),"json");
       return show(1,'登录成功');
       
    }
    //退出
    public function logout(){
         session("adminUser",null); 
         $this->redirect("/index.php?m=admin&c=login");
    }
}